/*
 * @(#)XCommandForm.java        1.0 2000/05/09
 *
 * This file is part of Xfuzzy 3.0, a design environment for fuzzy logic
 * based systems.
 *
 * (c) 2000 IMSE-CNM. The authors may be contacted by the email address:
 *                    xfuzzy-team@imse.cnm.es
 *
 * Xfuzzy is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * Xfuzzy is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

package xfuzzy.util;

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

/**
 * Componente para mostrar un barra de botones con la est�tica com�n
 * del entorno
 * 
 * @author Francisco Jos� Moreno Velo
 *
 */
public class XCommandForm extends JPanel implements ChangeListener {

	//----------------------------------------------------------------------------//
	//                           CONSTANTES PRIVADAS                              //
	//----------------------------------------------------------------------------//

	/**
	 * C�digo asociado a la clase serializable
	 */
	private static final long serialVersionUID = 95505666603003L;

	//----------------------------------------------------------------------------//
	//                            MIEMBROS PRIVADOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Lista de botones
	 */
	private JButton button[];

	//----------------------------------------------------------------------------//
	//                                CONSTRUCTOR                                 //
	//----------------------------------------------------------------------------//

	/**
	 * Constructor con los nombres de los botones
	 */
	public XCommandForm(String[] label) {
		super();
		setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));

		button = new JButton[label.length];

		Box form = new Box(BoxLayout.X_AXIS);
		form.add(Box.createGlue());
		form.add(Box.createHorizontalStrut(5));
		for(int i=0; i<label.length; i++) {
			button[i] = new JButton(label[i]);
			button[i].setBorder(BorderFactory.createRaisedBevelBorder());
			button[i].setHorizontalAlignment(JButton.CENTER);
			button[i].setFont(XConstants.font);
			button[i].setForeground(Color.black);
			button[i].setFocusPainted(false);
			button[i].setContentAreaFilled(false);
			button[i].addChangeListener(this);
			form.add(button[i]);
			form.add(Box.createGlue());
			form.add(Box.createHorizontalStrut(5));
		}

		add(Box.createVerticalStrut(5));
		add(form);
		add(Box.createVerticalStrut(5));
	}

	/**
	 * Constructor con los nombres y el controlador de los botones
	 */
	public XCommandForm(String[] label, ActionListener[] action) {
		this(label);
		for(int i=0; i<button.length; i++)
			if(action[i] != null) button[i].addActionListener(action[i]);
	}

	/**
	 * Constructor con los nombres/comandos y el controlador
	 */
	public XCommandForm(String[] label, ActionListener action) {
		this(label);
		for(int i=0; i<button.length; i++) if(action != null) {
			button[i].setActionCommand(label[i]);
			button[i].addActionListener(action);
		}
	}

	/**
	 * Constructor con los nombres, los comandos y el controlador
	 */
	public XCommandForm(String[] label, String[] command, ActionListener action) {
		this(label);
		for(int i=0; i<button.length; i++) if(action != null) {
			button[i].setActionCommand(command[i]);
			button[i].addActionListener(action);
		}
	}

	//----------------------------------------------------------------------------//
	//                             M�TODOS P�BLICOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Bloquea la altura de la barra de botones	
	 */
	public void block() {
		Dimension maxsize = getMaximumSize();
		Dimension prefsize = getPreferredSize();
		setMaximumSize(new Dimension(maxsize.width,prefsize.height));
	}

	/**
	 * Asigna el estado de habilitacion de un bot�n
	 */
	public void setEnabled(int i, boolean enable) {
		button[i].setEnabled(enable);
	}

	/**
	 * Asigna la anchura de un bot�n
	 */
	public void setCommandWidth(int width) {
		for(int i=0; i<button.length; i++) {
			Dimension prefsize = button[i].getPreferredSize();
			button[i].setPreferredSize(new Dimension( width, prefsize.height));
		}
	}

	/**
	 * Asigna el nombre de un bot�n
	 */
	public void setLabel(int i, String label) {
		button[i].setText(label);
	}

	/**
	 * Interfaz ChangeListener
	 */
	public void stateChanged(ChangeEvent e) {
		AbstractButton button = (AbstractButton) e.getSource();
		if(button.isSelected())
			button.setBorder(BorderFactory.createLoweredBevelBorder());
		else button.setBorder(BorderFactory.createRaisedBevelBorder());
	}
}

